<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">

      <el-form-item label="商品编码" prop="productCode">
        <el-input v-model="queryParams.productCode" placeholder="请输入商品编码" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="销售类型" prop="saleTypeCode">
        <el-select v-model="queryParams.saleTypeCode" placeholder="请选择销售类型" clearable size="small">
          <el-option v-for="dict in statusDictDatas"
                     :key="dict.value" :label="dict.label" :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所在仓库" prop="warehouseId">
        <el-select v-model="queryParams.warehouseId" filterable clearable placeholder="请选择所在仓库">
          <el-option v-for="warehouse in warehouseList" :key="parseInt(warehouse.id)" :label="warehouse.name"
                     :value="parseInt(warehouse.id)"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="IMEI" prop="imei">
        <el-input v-model="queryParams.imei" placeholder="请输入IMEI" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>

      <el-form-item label="销售人员" prop="saleUserName">
        <el-input v-model="queryParams.saleUserName" placeholder="请输入销售人员" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>

      <el-form-item label="销售时间" prop="saleTime">
        <el-date-picker v-model="queryParams.saleTime"  style="width: 240px"  value-format="yyyy-MM-dd HH:mm:ss"
                        type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
                        :default-time="['00:00:00', '23:59:59']"/>
      </el-form-item>
      <el-form-item label="退货时间" prop="returnTime">
        <el-date-picker v-model="queryParams.returnTime"  style="width: 240px"  value-format="yyyy-MM-dd HH:mm:ss"
                        type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
                        :default-time="['00:00:00', '23:59:59']"/>
      </el-form-item>
      <el-form-item label="报损时间" prop="breakageTime">
        <el-date-picker v-model="queryParams.breakageTime"  style="width: 240px"  value-format="yyyy-MM-dd HH:mm:ss"
                        type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
                        :default-time="['00:00:00', '23:59:59']"/>
      </el-form-item>

<!--      <el-form-item label="客户名称" prop="customerName">
        <el-input v-model="queryParams.customerName" placeholder="请输入客户名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="机况说明" prop="subrackDesc">
        <el-input v-model="queryParams.subrackDesc" placeholder="请输入机况说明" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">
              <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                         v-hasPermi="['pekingfish:storage-sale-record:create']">新增</el-button>
            </el-col>-->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                   :loading="exportLoading"
                   v-hasPermi="['pekingfish:storage-sale-record:export']">导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="loading" border :data="list">
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template v-slot="scope">
          <el-button size="mini" type="text" @click="handleUpdate(scope.row)"
                     v-hasPermi="['pekingfish:storage-sale-record:update']">详情
          </el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                               v-hasPermi="['pekingfish:storage-sale-record:delete']">删除</el-button>
        </template>
      </el-table-column>-->
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="商品编码" align="center" prop="productCode" width="180"/>
      <el-table-column label="手机品牌" align="center" prop="phoneBrand"/>
      <el-table-column label="手机型号" align="center" prop="phoneModel" width="180"/>
      <el-table-column label="IMEI" align="center" prop="imei"  width="150px"/>

      <el-table-column label="型号类型" align="center" prop="modelType"/>
      <el-table-column label="销售类型" align="center" prop="saleTypeName" width="100px"/>
      <el-table-column label="所在仓库" align="center" prop="warehouseName"/>
      <el-table-column label="销售价" align="center" prop="salePrice"/>
      <el-table-column label="退货价" align="center" prop="returnPrice"/>
      <el-table-column label="成本价" align="center" prop="costPrice"/>
      <el-table-column label="回收价" align="center" prop="paybackPrice"/>
      <el-table-column label="客户名称" align="center" prop="customerName"/>
      <el-table-column label="客户电话" align="center" prop="customerPhone" width="180"/>
      <el-table-column label="客户地址" align="center" prop="address"/>
<!--      <el-table-column label="机况说明" align="center" prop="subrackDesc" width="180"/>-->
      <el-table-column label="销售回收价" align="center" prop="salePaybackPrice" width="100"/>
      <el-table-column label="销售人员" align="center" prop="saleUserName"/>
      <el-table-column label="机况" align="center" prop="subrackDesc"/>
      <el-table-column label="仓库备注" align="center" prop="remark"/>
      <el-table-column label="入库时间" align="center" prop="inboundTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.inboundTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="销售时间" align="center" prop="saleTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.saleTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="退货时间" align="center" prop="returnTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.returnTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="报损时间" align="center" prop="breakageTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.breakageTime) }}</span>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="800px" v-dialogDrag append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" disabled>
        <el-row>
          <el-col :span="12">
            <el-form-item label="商品编码" prop="productCode">
              <el-input v-model="form.productCode"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机品牌" prop="phoneBrand">
              <el-input v-model="form.phoneBrand"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机型号" prop="phoneModel">
              <el-input v-model="form.phoneModel"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="型号类型" prop="modelType">
              <el-input v-model="form.modelType"/>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="销售类型" prop="saleTypeName">
              <el-input v-model="form.saleTypeName"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所在仓库" prop="warehouseName">
              <el-input v-model="form.warehouseName"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="销售价" prop="salePrice">
              <el-input v-model="form.salePrice"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="退货价" prop="returnPrice">
              <el-input v-model="form.returnPrice"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="成本价" prop="costPrice">
              <el-input v-model="form.costPrice"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="回收价" prop="paybackPrice">
              <el-input v-model="form.paybackPrice"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="客户名称" prop="customerName">
              <el-input v-model="form.customerName"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="客户电话" prop="customerPhone">
              <el-input v-model="form.customerPhone"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="客户地址" prop="address">
              <el-input v-model="form.address"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="机况说明" prop="subrackDesc">
              <el-input v-model="form.subrackDesc" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="销售回收价" prop="salePaybackPrice">
              <el-input v-model="form.salePaybackPrice"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="销售人员名称" prop="saleUserName">
              <el-input v-model="form.saleUserName"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="仓库备注" prop="remark">
              <el-input v-model="form.remark" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="销售时间" prop="saleTime">
              <el-date-picker clearable v-model="form.saleTime" type="date" value-format="timestamp"
                              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="退货时间时间" prop="returnTime">
              <el-date-picker clearable v-model="form.returnTime" type="date" value-format="timestamp"
                             />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="报损时间" prop="breakageTime">
              <el-date-picker clearable v-model="form.breakageTime" type="date" value-format="timestamp"
                              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="创建时间" prop="createTime">
              <el-date-picker clearable v-model="form.createTime" type="date" value-format="timestamp"/>
            </el-form-item>
          </el-col>

        </el-row>

      </el-form>
      <!--      <div slot="footer" class="dialog-footer">
              <el-button type="primary" @click="submitForm">确 定</el-button>
              <el-button @click="cancel">取 消</el-button>
            </div>-->
    </el-dialog>
  </div>
</template>

<script>
import {
  createStorageSaleRecord,
  updateStorageSaleRecord,
  deleteStorageSaleRecord,
  getStorageSaleRecord,
  getStorageSaleRecordPage,
  exportStorageSaleRecordExcel
} from "@/api/pekingfish/storageSaleRecord";
import {DICT_TYPE, getDictDatas} from "@/utils/dict";
import {getWarehouseList} from "@/api/pekingfish/warehouse";

export default {
  name: "StorageSaleRecord",
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 销售记录列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        createTime: [],
        productCode: null,
        phoneBrand: null,
        phoneModel: null,
        modelType: null,
        saleTypeCode: null,
        saleTypeName: null,
        salePrice: null,
        returnPrice: null,
        costPrice: null,
        paybackPrice: null,
        customerPhone: null,
        customerId: null,
        customerName: null,
        salePaybackPrice: null,
        saleUserId: null,
        saleUserNo: null,
        saleUserName: null,
        remark: null,
        saleTime: [],
        returnTime: [],
        breakageTime: [],
        warehouseCode: null,
        warehouseName: null,
        status: null,

      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      // 数据字典
      statusDictDatas: getDictDatas(DICT_TYPE.sale_type),
      //所在仓库
      warehouseList: [],
    };
  },
  created() {
    this.getList();
    this.getWarehouseList();
  },
  methods: {
    async getWarehouseList() {
      const {data} = await getWarehouseList();
      this.warehouseList = data;
    },

    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      getStorageSaleRecordPage(this.queryParams).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        productCode: undefined,
        phoneBrand: undefined,
        phoneModel: undefined,
        modelType: undefined,
        saleTypeCode: undefined,
        saleTypeName: undefined,
        salePrice: undefined,
        returnPrice: undefined,
        costPrice: undefined,
        paybackPrice: undefined,
        customerPhone: undefined,
        customerId: undefined,
        customerName: undefined,
        salePaybackPrice: undefined,
        saleUserId: undefined,
        saleUserNo: undefined,
        saleUserName: undefined,
        remark: undefined,
        saleTime: undefined,
        returnTime: undefined,
        breakageTime: undefined,
        warehouseCode: undefined,
        warehouseName: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加销售记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getStorageSaleRecord(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "销售记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.id != null) {
          updateStorageSaleRecord(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
          return;
        }
        // 添加的提交
        createStorageSaleRecord(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal.confirm('是否确认删除销售记录编号为"' + id + '"的数据项?').then(function () {
        return deleteStorageSaleRecord(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = {...this.queryParams};
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.$modal.confirm('是否确认导出所有销售记录数据项?').then(() => {
        this.exportLoading = true;
        return exportStorageSaleRecordExcel(params);
      }).then(response => {
        this.$download.excel(response, '销售记录.xls');
        this.exportLoading = false;
      }).catch(() => {
      });
    },
    choosewarehouse(warehouseId) {
      // 分公司名称
      getBranchCompanyByTenantId(warehouseId).then(response => {
        this.subTenants = response.data
      })
    },
  }
};
</script>
